/*
Package v1
@Time : 2022/12/20 22:48
@Author : 董胜烨
@File : employee
@Software: GoLand
@note:
*/
package v1

import (
	"employee/service"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func InsertEmployee(c *gin.Context) {
	eJson := &service.Employee{}
	err := c.BindJSON(eJson)
	if err != nil {
		log.Println("InsertEmployee Error", err)
	}

	ok := eJson.Insert()
	if ok {
		c.JSON(200, gin.H{
			"code": 0,
			"msg":  "插入成功",
		})
	} else {
		c.JSON(200, gin.H{
			"code": 1,
			"msg":  "插入失败",
		})
	}
}

func UpdateEmployee(c *gin.Context) {
	eJson := &service.Employee{}
	err := c.BindJSON(eJson)
	if err != nil {
		log.Println("UpdateEmployee Error", err)
	}
	ok := eJson.Update()
	if ok {
		c.JSON(200, gin.H{
			"code": 0,
			"msg":  "更新成功",
		})
	} else {
		c.JSON(200, gin.H{
			"code": 1,
			"msg":  "更新失败",
		})
	}
}
func DeleteEmployee(c *gin.Context) {
	eJson := &service.Employee{}
	err := c.BindJSON(eJson)
	if err != nil {
		log.Println("DeleteEmployee Error", err)
	}
	ok := eJson.Delete()
	if ok {
		c.JSON(200, gin.H{
			"code": 0,
			"msg":  "删除成功",
		})
	} else {
		c.JSON(200, gin.H{
			"code": 1,
			"msg":  "删除失败",
		})
	}

}
func QueryEmployee(c *gin.Context) {
	eJson := &service.Employee{}
	rest := eJson.QueryAll()
	if rest != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "获取成功",
			"data": rest,
			"code": 0,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "获取失败",
			"code": 1,
		})
	}
}
